/**
 *
 */
package org.suren.littlebird.actions.logger;

import org.suren.littlebird.server.LoggerServer;
import org.suren.littlebird.server.LoggerServerFactory;

/**
 * @author suren
 * @since 2015-2-3
 */
public class DefaultSuRenLoggerController implements SuRenLoggerController
{
	private LoggerServer server;

	@Override
	public int control(int action, String... names)
	{
		int counter = 0;
		if(names == null || names.length == 0)
		{
			return counter;
		}

		this.server = LoggerServerFactory.getInstance();
		if(server == null)
		{
			return counter;
		}

		for(String name : names)
		{
			switch(action)
			{
				case LEVEL_ALL:
					counter += (server.setLevel(name, "ALL") ? 1 : 0);
					break;
				case LEVEL_TRACE:
					counter += (server.setLevel(name, "TRANCE") ? 1 : 0);
					break;
				case LEVEL_DEBUG:
					counter += (server.setLevel(name, "DEBUG") ? 1 : 0);
					break;
				case LEVEL_INFO:
					counter += (server.setLevel(name, "INFO") ? 1 : 0);
					break;
				case LEVEL_WARN:
					counter += (server.setLevel(name, "WARN") ? 1 : 0);
					break;
				case LEVEL_ERROR:
					counter += (server.setLevel(name, "ERROR") ? 1 : 0);
					break;
				case LEVEL_OFF:
					counter += (server.setLevel(name, "OFF") ? 1 : 0);
					break;
			}
		}

		return counter;
	}

	@Override
	public int bridge(int action, String name, String host, int port)
	{
		int counter = -1;
		this.server = LoggerServerFactory.getInstance();
		if(server == null)
		{
			return counter;
		}

		switch(action)
		{
			case BRIDGE_ADD:
				counter = this.server.addBridge(name, host, port) ? 1 : -2;
				break;
			case BRIDGE_DEL:
				counter = this.server.removeBridge(name, host, port) ? 1 : -3;
				break;
			case BRIDGE_CLEAR:
				if(name == null || "".equals(name))
				{
					counter = this.server.clearBridges();
				}
				else
				{
					counter = this.server.clearBridges(name);
				}
				break;
		}

		return counter;
	}

}
